package com.example.appclient;

import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

public class Galeria extends Fragment {

	private TCPClient mTcpClient;
	
	public Galeria(){}
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_galeria, container, false);
        return rootView;
    
	}
	
	@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(2);
    }

	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_galeria);
	        final EditText editText = (EditText) findViewById(R.id.edit_message);
	        Button send = (Button)findViewById(R.id.send_button);        

	        // connect to the server
	        new connectTask().execute("");

	        send.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View view) {

	                String message = editText.getText().toString(); 

	                //sends the message to the server
	                if (mTcpClient != null) {
	                    mTcpClient.enviar_a_B(message);
	                }

	                editText.setText("");
	            }
	        });
	    }

	    public class connectTask extends AsyncTask<String,String,TCPClient> {

	        @Override
	        protected TCPClient doInBackground(String... message) {

	            mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
	                @Override
	                public void messageReceived(String message) {
	                    publishProgress(message);
	                }
	            });
	            mTcpClient.envio();

	            return null;
	        }

	        @Override
	        protected void onProgressUpdate(String... values) {
	            super.onProgressUpdate(values); 
	        }
	    }
	  }

